package com.thinkfit.jy.activity;

import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.utils.widget.ImageFilterView;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.common.lib.activity.BaseActivity;
import com.common.lib.bean.DeviceInfoBean;
import com.common.lib.bean.ProceduralTrainBean;
import com.common.lib.bean.VideoListBean;
import com.common.lib.constant.Constants;
import com.common.lib.constant.EventBusEvent;
import com.common.lib.fragment.BaseFragment;
import com.thinkfit.jy.R;
import com.thinkfit.jy.contract.ProceduralTrainingListContract;
import com.thinkfit.jy.presenter.ProceduralTrainingListPresenter;
import com.thinkfit.jy.utils.ShowSpeedViewUtils;
import com.thinkfit.jy.utils.SportsStatusBarViewUtils;
import com.thinkfit.jy.utils.ThemeUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 功率课程详情
 */
public class ProceduraTrainDetailActivity extends BaseActivity<ProceduralTrainingListContract.Presenter> implements ProceduralTrainingListContract.View {

    private ArrayList<BaseFragment> mFragments;
    ImageFilterView ivIcon;
    TextView tvSuitableCroudInfo,tvVideoTitle,tvVideoBrowse,tvVideoTime,tvVideoHeat,tvCourseDescription,tvVideoTrain;
    ImageView ivStar1,ivStar2,ivStar3,ivStar4,ivStar5;
    ConstraintLayout clMain;
    LinearLayout llChart;
    private VideoListBean mVideo;

    ArrayList<ImageView> ivList;
    int mType,vid;

    int[] colors;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_procedural_train_detail;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    public void init() {
        mVideo = (VideoListBean) getIntent().getSerializableExtra(Constants.BUNDLE_EXTRA);
        vid = mVideo.getId();
        colors = new int[]{getContext().getResources().getColor(R.color.color_45B4FF),
                getContext().getResources().getColor(R.color.color_36D16F),
                getContext().getResources().getColor(R.color.color_FFBB00),
                getContext().getResources().getColor(R.color.color_FF8A05),
                getContext().getResources().getColor(R.color.color_FF5959),
                getContext().getResources().getColor(R.color.color_AA51F0)};
        ivList = new ArrayList<>();
        setViewsOnClickListener(R.id.ll_sport_back);
        clMain = findViewById(R.id.cl_main);
        llChart = findViewById(R.id.ll_chart);
        ivIcon = findViewById(R.id.iv);
        tvSuitableCroudInfo = findViewById(R.id.tv_suitable_croud_info);
        tvVideoTitle = findViewById(R.id.tv_video_title);
        tvVideoBrowse = findViewById(R.id.tv_video_browse);
        tvVideoTime = findViewById(R.id.tv_video_time);
        tvVideoHeat = findViewById(R.id.tv_video_heat);
        tvVideoTrain = findViewById(R.id.tv_video_train);
        tvCourseDescription = findViewById(R.id.tv_course_description);
        ivStar1 = findViewById(R.id.iv_star1);
        ivStar2 = findViewById(R.id.iv_star2);
        ivStar3 = findViewById(R.id.iv_star3);
        ivStar4 = findViewById(R.id.iv_star4);
        ivStar5 = findViewById(R.id.iv_star5);

        ivList.add(ivStar1);
        ivList.add(ivStar2);
        ivList.add(ivStar3);
        ivList.add(ivStar4);
        ivList.add(ivStar5);
        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa    vid : "+vid);
//        if(vType == 1){
//            ((VideoCourseDetailContract.Presenter) getPresenter()).getVideoCourseDetail(mVideo.getVideoId(),vType);
//        }else {
//            ((VideoCourseDetailContract.Presenter) getPresenter()).getLiveVideoDetail(mVideo.getVideoId(),vType);
//        }

        for (DeviceInfoBean deviceInfoBean : mVideo.getBrustlist()){
//            items.add((float) deviceInfoBean.getDrap());
            setView(llChart,deviceInfoBean);
        }
        getPresenter().getWattinfo(vid);
    }

    public void initView(VideoListBean bean){
        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa   VideoListBean :  "+bean.toString());
        mVideo = bean;
        tvVideoTitle.setText(bean.getName());
        tvVideoTime.setText(getString(R.string.app_xx_minutes,bean.getMin()+""));
        tvVideoHeat.setText(getString(R.string.app_xx_kcal,bean.getKcal()+""));
        tvCourseDescription.setText(Html.fromHtml(bean.getDetail()));
        tvSuitableCroudInfo.setText(Html.fromHtml(bean.getFitman()));
        tvVideoBrowse.setText(getString(R.string.app_sport_video_xx_browse,bean.getBrowse()+""));
        tvVideoTrain.setText(Html.fromHtml(bean.getTrain()));
        for (int i = 0;i<ivList.size();i++){
            ivList.get(i).setImageResource(R.drawable.img_video_star_un);
            if(i < bean.getStar()){
                ivList.get(i).setImageResource(R.drawable.img_video_star);
            }
        }
    }

    public void setView(LinearLayout llChart,DeviceInfoBean deviceInfoBean){
        ViewGroup.LayoutParams layoutParams = llChart.getLayoutParams();
        int maxHight = layoutParams.height;
        int maxWidth = layoutParams.width;
        View item = LayoutInflater.from(getContext()).inflate(R.layout.item_histogram, null);
        View v1 = item.findViewById(R.id.v1);
        View v2 = item.findViewById(R.id.v2);

        ViewGroup.LayoutParams layoutParams1 = v1.getLayoutParams();

        layoutParams1.height = Math.round(maxHight*deviceInfoBean.getHeight());
        layoutParams1.width = Math.round(maxWidth*deviceInfoBean.getWidth());
        v1.setLayoutParams(layoutParams1);

        v2.setBackgroundColor(setColor(deviceInfoBean.getWatt()));

        llChart.addView(item);
    }

    public int setColor(int wait){
        if(wait<60){
            return colors[0];
        }else if(60<=wait && wait<=80){
            return colors[1];
        }else if(80<wait && wait<100){
            return colors[2];
        }else if(100<=wait && wait <110){
            return colors[3];
        }else if(110<=wait && wait <=120){
            return colors[4];
        }else {
            return colors[5];
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_sport_back:
                SportsStatusBarViewUtils.getInstance().isShowTarget(false);
                finish();
                break;
        }
    }

    public void onResume() {
        super.onResume();
        SportsStatusBarViewUtils.getInstance().isMain(false);
        clMain.setBackgroundResource(ThemeUtils.getInstance().getMainTheme());
        SportsStatusBarViewUtils.getInstance().isShowTarget(true);
        SportsStatusBarViewUtils.getInstance().isPlayVideo(true);
        ShowSpeedViewUtils.getInstance().goneLayout();
    }

    @Override
    protected void onStop() {
        super.onStop();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SportsStatusBarViewUtils.getInstance().isPlayVideo(false);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceive(HashMap<String, Object> map) {
        if (map == null) {
            return;
        }
        if(map.containsKey(EventBusEvent.TO_VIDEO)){
            Bundle bundle = new Bundle();
            bundle.putSerializable(Constants.BUNDLE_EXTRA, mVideo);
            openActivity(ProceduralTrainInfoActivity.class, bundle);
        }else {
            super.onReceive(map);
        }
    }

    @NonNull
    @Override
    protected ProceduralTrainingListContract.Presenter onCreatePresenter() {
        return new ProceduralTrainingListPresenter(this);
    }

    @Override
    public boolean isMainActivity() {
        return false;
    }

    @Override
    public void getVideoListSuccess(ProceduralTrainBean bean) {

    }

    @Override
    public void getWattinfoSuccess(VideoListBean bean) {
        initView(bean);
    }

    @Override
    protected void onCreated(@Nullable Bundle savedInstanceState) {

    }
}